<?php
/**
 * @title: 登录逻辑
 * @desc:
 * @since：2019/6/27 0027  下午 3:46
 */
namespace app\admin\logic;

use app\common\logic\Base;
use app\common\tools\ReturnCode;
use app\common\tools\Crypto;
use app\model\Admin;
/**
 * Admin基础逻辑 extends Base
 */
class Login extends  Base
{


    /**
     * 登录处理
     */
    static public function loginHandle($username, $password, $verify){
        if (!$username) {
            return self::buildFailed(ReturnCode::LOGIN_ERROR[0], ReturnCode::LOGIN_ERROR[1]);
        }
        if (!$password) {
            return self::buildFailed(ReturnCode::LOGIN_ERROR[0], ReturnCode::LOGIN_ERROR[1]);
        } else {
            $password = Crypto::userMd5($password);
        }

        $where = [ 'admin_name' => $username, 'password' => $password];
        $field = ['admin_id','admin_name','nickname'];

        $adminInfo = Admin::field($field)->where($where)->find();

        //$data = ["info"=>$info];

        $userInfo['access'] = \app\admin\logic\RoleAdmin::getAccess($adminInfo["admin_id"]);//$this->getAccess($userInfo['id']);

        $MtToken = md5(uniqid() . time());
        cache('Login:' . $MtToken, json_encode($userInfo), config('apiadmin.ONLINE_TIME'));
        cache('Login:' . $userInfo['id'], $MtToken, config('apiadmin.ONLINE_TIME'));

        $userInfo['MtToken'] = $MtToken;

        return self::buildSuccess($userInfo, ReturnCode::LOGIN_SUCCESS[0],ReturnCode::LOGIN_SUCCESS[1]);
    }

    public function logout(){
        //$MtToken = $request->header('MtToken', '');

        $MtToken = $this->request->header('MtToken');
        cache('Login:' . $MtToken, null);
        cache('Login:' . $this->userInfo['id'], null);

        return self::buildSuccess([], ReturnCode::LOGOUT_SUCCESS[0],ReturnCode::LOGOUT_SUCCESS[1]);
    }


}